package at.ac.tuwien.sepm.s2011.e0208329.dao;

import static org.junit.Assert.*;
import at.ac.tuwien.sepm.s2011.e0208329.db.DatabaseManager;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Dienstleistung;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Rechnung;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;



public class RechnungDAOImplTest {
	private DatabaseManager dbm;
	private RechnungDAOImpl rechnungDAO;
	private static final Logger log = Logger.getLogger(DienstleistungDAOImpl.class);

	@Before
	public void setUp() throws Exception {
		PropertyConfigurator.configure("log4j.properties");
		dbm = DatabaseManager.getDatabaseManager(); //initialize dbm
		dbm.getConnection().setAutoCommit(false); // set the auto-commit of connection to false, needed for rollback()
		rechnungDAO = DAOFactoryImpl.getDAOFactory().getBillDAO();
		
	}

	@After
	public void tearDown() throws SQLException {
		dbm.getConnection().rollback();
	}

	@Test(expected = IllegalArgumentException.class)
	public void testInsertBill() throws SQLException{
		Rechnung rec= null;
		rechnungDAO.insertBill(rec);
	}
	

	@Test
	public void testGetAllBills() throws SQLException{
		ArrayList<Rechnung> rec= rechnungDAO.getAllBills();
		assertNotNull(rec);
		assertTrue(rec.size() > 0 );
	}
	
	@Test
	public void testInsertBill_GetAllBills() throws SQLException{
		String sqlStmt = "DELETE FROM RechnungItem";
		dbm.update(sqlStmt);
		sqlStmt = "DELETE FROM Rechnung";
		dbm.update(sqlStmt);   // clear the tables from Database
		Rechnung rechnung = new Rechnung("SL","ccc");
		rechnungDAO.insertBill(rechnung); // try insert
		ArrayList<Rechnung> rec=rechnungDAO.getAllBills(); // try getAll
		assertEquals(1, rec.size());
	}
	
	@Test
	public void testGetAllServiceIDForBill() throws SQLException{
         Map<Integer, Integer> rec=rechnungDAO.getAllServiceIDForBill(1); // where only 1 there, serviceID should be 1
		 assertFalse(rec.isEmpty());
		 assertTrue(rec.size() > 0 );

	}


}
